package com.xxx.video.main.adapters;

import android.content.Context;
import android.graphics.Color;
import android.view.View;

import androidx.annotation.NonNull;

import com.xxx.video.R;
import com.xxx.video.commen.adapters.BaseRecycleViewAdapter;
import com.xxx.video.commen.adapters.BaseViewHolder;
import com.xxx.video.commen.utils.ListUtils;
import com.xxx.video.commen.views.banner.Banner;
import com.xxx.video.commen.views.banner.IndicatorView;
import com.xxx.video.main.beans.IndexBean;

import java.util.List;

import butterknife.BindView;

public class HomeFragmentBannerAdapter extends BaseRecycleViewAdapter<Object, HomeFragmentBannerAdapter.MyHolder> {
    /**
     * banner data
     */
    private List<IndexBean.CarouselListBean> carouselList;
    private HomeFragmentBannerItemAdapter mTopicDetailsBannerAdapter;

    public HomeFragmentBannerAdapter(@NonNull Context context) {
        super(context);
    }

    @Override
    public MyHolder onCreateDefaultViewHolder(View view, int viewType) {
        return new MyHolder(view);
    }

    @Override
    public int getItemLayout(int viewType) {
        return R.layout.item_home_fragment_banner;
    }

    @Override
    public int getItemCount() {
        return 1;
    }

    /**
     * init banner data
     *
     * @param carouselList data
     */
    public void setBannerData(List<IndexBean.CarouselListBean> carouselList) {
        this.carouselList = carouselList;
        notifyDataSetChanged();
    }

    class MyHolder extends BaseViewHolder {

        @BindView(R.id.bn_item_home_fragment_banner)
        Banner bnItemHomeFragmentBanner;

        public MyHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void setData2View(int position) {
            if (ListUtils.isEmpty(carouselList)) {
                return;
            }
            IndicatorView indicator = new IndicatorView(context)
                    .setIndicatorColor(Color.DKGRAY)
                    .setIndicatorSelectorColor(Color.WHITE);
            if (mTopicDetailsBannerAdapter == null) {
                mTopicDetailsBannerAdapter = new HomeFragmentBannerItemAdapter(context);
                bnItemHomeFragmentBanner.setIndicator(indicator).setAdapter(mTopicDetailsBannerAdapter);
            }
            mTopicDetailsBannerAdapter.setData(carouselList);
            mTopicDetailsBannerAdapter.setOnItemClickListener((OnItemClickListener<IndexBean.CarouselListBean>) (carouselListBean, position1, type) -> {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(carouselListBean, position1, type);
                }
            });
        }
    }
}
